﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GDI_应用案例.Pages
{
    [FormPage(8, "圆弧测量ROI")]
    public partial class Page圆弧测量ROI : PageBase
    {
        public Page圆弧测量ROI()
        {
            InitializeComponent();
        }
        CircleROI _circleROI = new CircleROI();

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            _circleROI.DrawROI(e.Graphics);
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            _circleROI.MouseDown(e);
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            _circleROI.MouseMove(e);
            this.Invalidate();
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            _circleROI.MouseUp(e);
        }
    }

    public class CircleROI : ROIBase
    {
        private Point center=new Point(150,150);
        private int outerRadius = 100;
        private int innerRadius = 80;
        private Point controlPoint;
        private bool isDragging = false;
        private const int controlPointRadius = 5;

        public override void DrawROI(Graphics g)
        {
            // 绘制外圆
            using (Pen pen = new Pen(Color.Blue, 2))
            {
                g.DrawEllipse(pen,
                    center.X - outerRadius,
                    center.Y - outerRadius,
                    outerRadius * 2,
                    outerRadius * 2);
            }

            // 绘制内圆
            using (Pen pen = new Pen(Color.Blue, 2))
            {
                g.DrawEllipse(pen,
                    center.X - innerRadius,
                    center.Y - innerRadius,
                    innerRadius * 2,
                    innerRadius * 2);
            }

            // 绘制控制点
            using (Brush brush = new SolidBrush(Color.Red))
            {
              g.FillEllipse(brush,
                    controlPoint.X - controlPointRadius,
                    controlPoint.Y - controlPointRadius,
                    controlPointRadius * 2,
                    controlPointRadius * 2);
            }
        }

        public override void MouseDown(MouseEventArgs e)
        {
            if (IsInControlPoint(e.Location))
            {
                isDragging = true;
            }
        }

        public override void MouseMove(MouseEventArgs e)
        {
            //if (isDragging)
            //{
            //    // 计算新的半径（限制最小和最大尺寸）
            //    int newRadius = Math.Abs(e.X - center.X);
            //    int minRadius = innerRadius + 20;  // 保持最小环宽
            //    int maxRadius = Math.Min(center.X, center.Y) - 10;

            //    outerRadius = Math.Clamp(newRadius, minRadius, maxRadius);
            //    UpdateControlPoint();
            //    pictureBox.Invalidate();
            //}
            //else
            //{
            //    Cursor = IsInControlPoint(e.Location) ? Cursors.SizeWE : Cursors.Default;
            //}
        }

        public override void MouseUp(MouseEventArgs e)
        {
            isDragging = false;
        }
        private bool IsInControlPoint(Point point)
        {
            return Math.Sqrt(Math.Pow(point.X - controlPoint.X, 2) +
                           Math.Pow(point.Y - controlPoint.Y, 2)) < controlPointRadius + 5;
        }

    }
    public abstract class ROIBase
    {
        public abstract void MouseDown(MouseEventArgs e);
        public abstract void MouseMove(MouseEventArgs e);
        public abstract void MouseUp(MouseEventArgs e);
        public abstract void DrawROI(Graphics g);
    }
}
